/**
 * Created by chenxiaopeng on 2018/3/6.
 */
import React from 'react';

export function index(Component, route) {
  if (Component.AuthenticatedComponent) {
    return Component.AuthenticatedComponent;
  }

  class AuthenticatedComponent extends React.Component {
    static contextTypes = {
      router: React.PropTypes.object.isRequired,
    };

    state = {
    };

    componentWillMount() {
      this.checkBrow();
    }

    componentWillReceiveProps() {
      this.checkBrow();
    }

    isAlipay = () => {
      const userAgent = navigator.userAgent.toLowerCase(); // eslint-disable-line

      if (userAgent.match(/Alipay/i) == 'alipay') { // eslint-disable-line
        return true;
      } else {
        return false;
      }
    };

    isWeixn = () => {
      const ua = navigator.userAgent.toLowerCase(); // eslint-disable-line
      if (ua.match(/MicroMessenger/i) == 'micromessenger') { // eslint-disable-line
        return true;
      } else {
        return false;
      }
    };

    checkBrow() {
      if (this.isWeixn() && route !== 'WC') {
        this.context.router.history.push('/WC');
      } else if (this.isAlipay() && route !== 'AP') {
        this.context.router.history.push('/AP');
      }
    }

    render() {
      return <Component {...this.props} />;
    }
  }

  Component.AuthenticatedComponent = AuthenticatedComponent; // eslint-disable-line
  return Component.AuthenticatedComponent;
}
